from connector import DatabaseDriver


def register_user(driver: DatabaseDriver, username: str, password: str, role: int):
    result: tuple = driver.queryAll(
        'select user_id from user where user_name = "{}"'.format(username))
    if result is None or len(result) == 0:
        update: int = driver.update(
            'insert into user (user_name,user_password,user_role) values("{}","{}",{})'.format(username, password,
                                                                                               role))
    else:
        return 0
    return update


def login(driver: DatabaseDriver, username: str, password: str) -> tuple:
    result: tuple = driver.query(
        'select user_id,user_role from user where user_name = "{}" and user_password="{}"'.format(username, password))
    return result

def get_username_by_user_id(driver: DatabaseDriver, user_id: int):
    return driver.query('select user_name from user where user_id ="{}"'.format(user_id))
